<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 后台配置控制器
 * @author yangweijie <yangweijiester@gmail.com>
 */
class BidPriceController extends AdminController {

    /**
     * 竞价类型管理
     * @return none
     */
    public function BidType(){
        $list   = $this->lists(M('BidType'), array(),'id asc');
        $this->assign('_list', $list);
        $this->meta_title = '竞价类型';
        $this->display();
    }

    public function addBidType(){
        $this->meta_title = '新增竞价类型';
        $this->display('edit_bidtype');
    }

    public function editBidType(){
        $id=I('get.id');
        $data=M('BidType')->where(array('id'=>$id))->find();
        $this->assign('info',$data);
        $this->meta_title = '编辑竞价类型';
        $this->display('edit_bidtype');
    }

    public function saveBidType(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }

        if(empty($data['id'])){ //新增数据
            $res = M('BidType')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('BidType') );
            }
        } else { //更新数据
            $status = M('BidType')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('BidType') );
            }
        }
    }


    /**删除竞价分类，同时删除下面的所有竞价场次
     * @param array $where
     */
    public function deleteBidType() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $where['id']=array('in',$ids);
            $map['cate_id']=array('in',$ids);
        }else{
            $where['id']=$ids;
            $map['cate_id']=$ids;
        }

        if( M('bid_type')->where($where)->delete() !== false &&  M('bid_list')->where($map)->delete() !== false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }

    }



    /**
     * 竞价管理
     * @return none
     */
    public function bidList(){
        $where=array();
        I('cate_id') && $where['cate_id']=I('cate_id') ;
        I('end_time') && $where['end_time']=array(I('end_time'),time()) ;

        $list   = $this->lists(M('BidList'),  $where,'id asc');
        $cateList = M("bid_type")->select();
        $cateList=get_id_key($cateList,'id');
        $this->assign('cateList',$cateList);
        $this->assign('_list', $list);
        $this->meta_title = '竞价类型';
        $this->display();
    }

    public function addBid(){
        $cateList = M("bid_type")->select();
        $this->assign('cateList',$cateList);
        $this->meta_title = '新增竞价';
        $this->display('edit_bid');
    }

    public function editBid(){
        $id=I('get.id');
        $data=M('BidList')->where(array('id'=>$id))->find();
        $data['end_time']=date('Y-m-d H:i:s',$data['end_time']);
        $cateList = M("bid_type")->select();
        $this->assign('cateList',$cateList);
        $this->assign('info',$data);
        $this->meta_title = '编辑竞价';
        $this->display('edit_bid');
    }

    public function saveBid(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        $data['end_time']=strtotime($data['end_time']);
        if(empty($data['id'])){ //新增数据
            $data['start_time']=time();
            $data['bid_sn']=getGoodsNumber();
            $res = M('BidList')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('bidList') );
            }
        } else { //更新数据
            $status = M('BidList')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('bidList') );
            }
        }
    }


    /**删除竞价单，同时删除竞价下面的商品
     * @param array $where
     */
    public function deleteBid() {
        $ids    =   I('request.ids');
        if(empty($ids)){
            $this->error('请选择要操作的数据');
        }
        if(is_array($ids)){
            $where['id']=array('in',$ids);
            $map['bid_id']=array('in',$ids);
        }else{
            $where['id']=$ids;
            $map['bid_id']=$ids;
        }

        if( M('bid_list')->where($where)->delete() !== false &&  M('bid_good')->where($map)->delete() !== false) {
            $this->success('删除成功','',true);
        }else{
            $this->error('删除失败！','',true);
        }

    }


    /**
     * 竞价商品管理
     * @return none
     */
    public function bidGoods(){
        $where=array();
        I('bid_id') && $where['bid_id']=I('bid_id') ;
        $bid_info=M('bid_list')->where(array('id'=>I('bid_id')))->find();
        $list   = $this->lists(M('bid_good'), $where,'id asc');
        $this->assign('bid_info', $bid_info);
        $this->assign('_list', $list);
        $this->meta_title = '竞价类型';
        $this->display();
    }

    public function addGoods(){
        $goodList = M("goods")->field('id,goods_name')->select();
        $this->assign('goodList',$goodList);
        $this->meta_title = '添加竞价商品';
        $this->display('edit_goods');
    }

    public function editGoods(){
        $id=I('get.id');
        $data=M('bid_good')->where(array('id'=>$id))->find();
        $goodList = M("goods")->field('id,goods_name')->select();
        $this->assign('goodList',$goodList);

        $this->assign('info',$data);
        $this->meta_title = '编辑';
        $this->display('edit_goods');
    }

    public function saveGoods(){
        $data=I('post.');
        if(empty($data)){
            $this->error ("请填写完整数据");
        }
        if(empty($data['id'])){ //新增数据
            $res = M('bid_good')->add($data);
            if(!$res){
                $this->error ("新增出错");
            }else{
                $this->success ('新增成功！', U('bidGoods',array('bid_id'=>$data['bid_id'])));
            }
        } else { //更新数据
            $status = M('bid_good')->save($data);
            if(false === $status){
                $this->error ("更新出错");
            }else{
                $this->success ('更新成功！', U('bidGoods',array('bid_id'=>$data['bid_id'])) );
            }
        }
    }



   public function bidUsers(){
        $where=array();
        if( I('bid_id')){
            $where['bid_id']=I('bid_id') ;
        }

        I('get.good_id') && $where['good_id']=I('good_id');
        
       /*  $bid=M('BidList')->where(array('id'=>$where['bid_id']))->find();
        switch ($bid['cate_id']) {
            case '1':
                $list   = $this->lists(M('bid_user'), $where,'user_price desc,create_time asc');
                $tpl='bidusers_cate1';
                break;
            case '2':
                $list   = $this->lists(M('bid_user'), $where,'per_price desc,create_time asc');
                $tpl='bidusers_cate2';
                break;
        }     */
        $list   = $this->lists(M('bid_user'), $where,'user_price desc,create_time asc');   
        $this->assign('_list', $list);
        $this->meta_title = '用户报价';
        $this->display('bidusers_cate2');
   }


   //所有竞价用户
   public function allBidUsers(){
        if( I('bid_id')){
            $where['bid_id']=I('bid_id') ;
        }
        $bid=M('BidList')->where(array('id'=>$where['bid_id']))->find();

        I('get.good_id') && $where['good_id']=I('good_id');
        
       switch ($bid['cate_id']) {
            case '1':
                $list   = $this->lists(M('bid_user'), $where,'user_price desc,create_time asc');
                $tpl='bidusers_cate1';
                break;
            case '2':
                $list   = $this->lists(M('bid_user'), $where,'per_price desc,create_time asc');
                $tpl='bidusers_cate2';
                break;
      }        
        $this->assign('_list', $list);
        $this->assign('bid', $bid);
        $this->meta_title = '竞价类型';
        $this->display($tpl);
   }


   //查看用户订单
   public function orderList(){
      $id=I('get.id');
      $bid_user=M('bid_user')->where(array('id'=>$id))->find();
      $bid_info=M('bid_list')->where(array('id'=>$bid_user['bid_id']))->find();
      $order=D('Order')->createOrder($bid_info['id'],$bid_info['cate_id'],$bid_info['bid_sn'],$bid_user);
      $arr[]=$order;
      $this->assign('_list',$arr);
      $this->display();
   }

   public function order(){
        $where=array();
        I('status') && $where['status']=I('status') ;
        $list   = $this->lists(M('order_list'), $where,'id asc');
        $this->assign('_list', $list);
        $this->meta_title = '订单列表';
        $this->display('orderlist');
   }

    //修改订单状态
   public function setOrderStatus(){
       $where['id']=I('id');
       if( M('order_list')->where($where)->setField('status',I('status'))) {
           $this->success('状态修改成功','',true);
       }else{
           $this->error('状态修改失败！','',true);
       }
   }


    //查看退货单
    public function returnList(){
        $where=array();
        I('status') && $where['status']=I('status') ;
        $list   = $this->lists(M('return_list'),  $where,'create_time asc');
        $this->assign('_list',$list);
        $this->display();
    }

    //处理退货单
    public function handelReturn(){
       if(IS_POST){
          $data=I('post.');
          if(empty($data)){
               $this->error ("请填写完整数据");
           }
           $data['return_fail']=$data['return_count']-$data['return_suceess'];
           $data['status']=2;

           $arr['return_count']=$data['return_suceess'];
           $arr['return_price']=$data['return_success_price'];

           $row=M('order_list')->where(array('id'=>$data['order_id']))->save($arr);

           $status = M('return_list')->save($data);
            if(false !== $status && $row!==false){
                $this->success ('处理成功！', U('returnList'));
            }else{
                $this->error ("处理出错");
            }
       }else{
          $id=I('get.id');
          $data=M('return_list')->where(array('id'=>$id))->find();
          if($data['status']==2){
            $this->success('已处理',U('returnList'));
          }

          $order_info=M('order_list')->where(array('id'=>$data['order_id']))->find();

          $this->assign('order_info',$order_info);
          $this->assign('info',$data);
          $this->display();
       }
    }


    


}
